﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using S3.DataModel;
using S3.Common;

namespace S3.Biz
{
    public class TimingServiceConfigurationManager : CommonManager
    {
        #region GetKindOfMachineByDesk
        /// <summary>
        /// Get Kind Of Machine By Desk
        /// </summary>
        /// <param name="shortName"></param>
        /// <param name="isDeleted"></param>
        /// <returns></returns>
        public string[] GetKindOfMachineByDesk(string deskName)
        {
            string[] result = null;

            //Check parameter
            if (string.IsNullOrEmpty(deskName)) return result;

            try
            {
                //
                var list = _entities.TimingServiceConfigurations.Where(a => a.TimingServiceName == deskName);
                if (list != null)
                {
                    var strKindOfMachines = list.Select(a => a.Types).FirstOrDefault();
                    result = strKindOfMachines.Split(',');
                    //foreach (var machine in listMachines)
                    //{
                    //    if (!string.IsNullOrEmpty(machine))
                    //    {
                    //        MachineTypes machineTypes = new MachineTypes()
                    //        {
                    //            Type = machine.Trim()
                    //        };

                    //        result.Add(machineTypes);
                    //    }
                    //}
                }

                return result;

            }
            catch (Exception ex)
            {
                using (var exception = new SSDLoggingHandler())
                {
                    exception.BizException("DeskConfigurationManager::GetKindOfMachineByDesk::" + deskName, ex, true);
                }
            }

            return result;
        }



        #endregion

        public List<TimingServiceConfiguration> GetAllAvaiableDesks()
        {
            List<TimingServiceConfiguration> listDesks = new List<TimingServiceConfiguration>();
            try
            {
                var ready = DeskStatusValue.Text[DeskStatus.Avaiable];
                var query = from item in _entities.TimingServiceConfigurations
                            where item.Status.Equals(ready, StringComparison.OrdinalIgnoreCase)
                            orderby item.TimingServiceName
                            select item;

                listDesks = query.ToList();

                _entities.Dispose();
            }
            catch (Exception ex)
            {
                using (var exception = new SSDLoggingHandler())
                {
                    exception.BizException("DeskConfigurationManager::GetAllAvaiableDesks::", ex, true);
                }
            }

            return listDesks;
        }

        public List<TimingServiceConfiguration> GetAllDesks()
        {
            List<TimingServiceConfiguration> listDesks = new List<TimingServiceConfiguration>();
            try
            {
                var query = from item in _entities.TimingServiceConfigurations
                            orderby item.TimingServiceName
                            select item;

                listDesks = query.ToList();

                _entities.Dispose();
            }
            catch (Exception ex)
            {
                using (var exception = new SSDLoggingHandler())
                {
                    exception.BizException("DeskConfigurationManager::GetAllDesks::", ex, true);
                }
            }

            return listDesks;
        }

        /// <summary>
        /// Delete all and save all to DB
        /// </summary>
        /// <param name="inputList"></param>
        public void Save(List<TimingServiceConfiguration> inputList)
        {
            try
            {
                //Delete all old items
                var oldList = GetAll().ToList();
                oldList.ForEach(_entities.DeleteObject);
                _entities.SaveChanges();

                if (inputList != null && inputList.Count > 0)
                {
                    foreach (var item in inputList)
                    {
                        item.ModifiedBy = Common.Constants.StaffName;
                        item.ModifiedDate = DateTime.Now;

                        //Insert
                        _entities.AddToTimingServiceConfigurations(item);
                    }

                    //Submit change
                    _entities.SaveChanges();
                    //ActionLogger.LogAction(ActionType.ChangeMachineQuantity, Constants.StaffName, DateTime.Now, new string[] { });
                }

            }
            catch (Exception ex)
            {
                using (var exception = new SSDLoggingHandler())
                {
                    exception.BizException("DeskConfigurationManager::Save::", ex, true);
                }
            }

        }

        /// <summary>
        /// Load tat ca danh sach ban
        /// </summary>
        /// <param name="deskName"></param>
        /// <returns></returns>
        #region GetAll
        public IQueryable<TimingServiceConfiguration> GetAll()
        {
            try
            {
                return _entities.TimingServiceConfigurations.OrderBy(i=>i.TimingServiceName);

            }
            catch (Exception ex)
            {
                using (var exception = new SSDLoggingHandler())
                {
                    exception.BizException("DeskConfigurationManager::GetAll::", ex, true);
                }
            }

            return null;
        }
        #endregion
    }
}
